package test.eventDrivenModel;

public class Client {
	
	private Event currentEvent;
	
	private Button button;
	
	
	public void initComponent() {
		
		button = new Button();
		
		button.addEventListener(new ClickEventHandler() {
			@Override
			public void handleEvent(ClickEvent event) {
				System.out.println("Button was clicked!");
			}
		});
		
		button.addEventListener(new DblClickEventHandler() {
			@Override
			public void handleEvent(DblClickEvent event) {
				System.out.println("Button was double clicked!");
			}
		});
		
	}
	
	public void testCommonEvents() {
		currentEvent = new ClickEvent();
		button.notifyListeners(currentEvent);
		//Button类中的notifyListener方法实现起来虽方便，利用了一把异常机制，但着实不推荐大家在项目中这样做。且不说性能问题，Listener的handleEvent方法里所有抛出的ClassCastException都需要重新包装。
		//当然，我们可以使用annotation、限定类名等方式相对优雅地解决event和对应listener的mapping问题。
		currentEvent = new DblClickEvent();
		button.notifyListeners(currentEvent);
	}
	public static void main(String[] args){
		Client c=new Client();
		c.initComponent();
		c.testCommonEvents();
	}

}